var suggestTags = new Array();
var complete;

function initTags() {
	if ($('tags').value) {
		updateHighlight();
	}
	
	$('tags').onkeypress = function(event) { e = (event || window.event); if (e.keyCode == 9 && complete) { completeTag(complete); Event.stop(e); } };
	$('tags').onkeyup = findTags;
		
	if (suggestTags.length > 0) {
		return;
	}

	if ($('tageditor').className == 'prefilled') {
		Element.hide($('tageditor'));
	} else {
		if ($('usersuggest')) {
			var user = $('usersuggest').childNodes;
			for (i=0; i<user.length; i++) {
				if (user[i].className == "suggest tagname") {
					suggestTags.push(user[i].innerHTML);
				}
			}
		}
		
		if ($('globalsuggest')) {
			var global = $('globalsuggest').childNodes;
			for (i=0; i<global.length; i++) {
				if (global[i].className == "suggest tagname") {
					k = false;
					for (j=0; j<suggestTags.length; j++) {
						if (suggestTags[j] == global[i].innerHTML) {
							k = true;
						}
					}
					
					if (!k) {
						suggestTags.push(global[i].innerHTML);
					}
				}
			}
		}
	}
}

function editTags() {
	$('tageditor').className = '';
	Element.hide($('edittags'));
	Element.show($('tageditor'));
	initTags();
}

function getTags() {
	var tags = $('tags').value.toLowerCase().split(" ");
	for (i=0; i<tags.length; i++) {
		if (!tags[i]) {
			delete(tags[i]);
		}
	}
	
	return tags;
}

function updateTagList(tags) {
	var t = new Array();
	for (i=0; i<tags.length; i++) {
		if (tags[i]) {
			t.push(tags[i]);
		}
	}
	$('tags').value = (t.join(" ")).replace(/^\s*|\s*$/g,"")+" ";
	$('tags').focus();
	$('tagssuggest').innerHTML = '&nbsp;';
}

function toggleTag(tag) {
	var tags = getTags();
	var exists = false;
	for (i=0; i<tags.length; i++) {
		if (tags[i] == tag) {
			delete(tags[i]);
			exists = true;
		}
	}
	if (!exists) {
		tags.push(tag);
	}
	updateTagList(tags);
	updateHighlight();
	return false;
}

function completeTag(tag) {
	var tags = getTags();
	tags[tags.length-1] = tag;
	updateTagList(tags);
	updateHighlight();
	return false;
}

function findTags(e) {
	var tags = $('tags').value.toLowerCase().split(" ");
	complete = '';
	
	var term = tags[tags.length-1];
	var suggest = '';
	if (term.length > 0) {
		for (i=0; i<suggestTags.length; i++) {
			if (suggestTags[i].substring(0, term.length) == term && term.length != suggestTags[i].length) {
				var used = false;
				for (j=0; j<tags.length; j++) {
					if (tags[j] == suggestTags[i]) {
						used = true;
					}
				}
				
				if (!used) {
					if (suggest=='') {
						complete = suggestTags[i];
						suggest += '<a href="#" class="tagname autocomplete" onclick="javascript:return completeTag(\''+suggestTags[i]+'\')" title="Press tab to complete this tag.">'+suggestTags[i]+'</a> ';
					} else {
						suggest += '<a href="#" class="tagname" onclick="javascript:return completeTag(\''+suggestTags[i]+'\')">'+suggestTags[i]+'</a> ';
					}
				}
			}
		}
	}
		
	if (!suggest) {
		suggest = '&nbsp;';
	}
	
	$('tagssuggest').innerHTML = suggest;
	
	updateHighlight();
}

function updateHighlight() {
	var tags = getTags();
	var suggested = document.getElementsByClassName('suggest');
	
	for (i=0; i<suggested.length; i++) {
		highlight = false;
		for (j=0; j<tags.length; j++) {
			if (tags[j] == suggested[i].innerHTML) {
				highlight = true;
			}
		}
	
		if (highlight) {
			suggested[i].className = 'suggest tagname highlight';
		} else {
			suggested[i].className = 'suggest tagname';
		}
	}
}

Event.observe(window, 'load', initTags, false);